import { Result, ResultError } from "@/types";
import { ElMessageBox } from "element-plus";
import { Ref, ref, reactive, watch, getCurrentInstance } from "vue";
import { receiptOrderRefund, arrivalOrderRefund, receiptAndReFundOrderRefund } from "@/api/store/order_refund";
export default function (_call: any) {

    const { proxy } = getCurrentInstance() as any;

    const {
        proxy: { $utils },
    } = getCurrentInstance() as any;

    //审核
    const _onAudit = (id: number) => {
        proxy?.$refs["auditRef"].open(id);
    };
    // 确定已收货
    const _onReceipt = (id: number) => {
        ElMessageBox.confirm("确定已收到用户寄回的商品吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
        }).then(() => {
            receiptOrderRefund({
                id,
            })
                .then((res: Result) => {
                    if (typeof _call == "function") {
                        _call()
                    }
                    $utils.successMsg(res);
                })
                .catch((err: ResultError) => {
                    $utils.errorMsg(err);
                });
        });
    };

    // 发货
    const _onDelivery = (id: number) => {
        proxy?.$refs["deliveryRef"].open(id);
    }

    // 确定收货并处理退款
    const _onReceiptAndReFund = (id: number, detail: any) => {
        ElMessageBox.prompt(`商品金额:￥${detail.order_goods.total_pay_price}，订单实付：￥${detail.order.pay_price}`, "确定收货用户退回的商品及执行退款吗?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            inputPattern:
                /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            inputErrorMessage: '请输入退款金额',
            inputPlaceholder: "请输入退款金额",
            center: true,
        }).then((res: any) => {
            receiptAndReFundOrderRefund({
                id,
                refund_money: res.value
            })
                .then((res: Result) => {
                    if (typeof _call == "function") {
                        _call()
                    }
                    $utils.successMsg(res);
                })
                .catch((err: ResultError) => {
                    $utils.errorMsg(err);
                });
        });
    }

    // 用户已到店
    const _onArrival = (id: number, detail: any) => {
        ElMessageBox.prompt(`商品金额:￥${detail.order_goods.total_pay_price}，订单实付：￥${detail.order.pay_price}`, "确定用户已到店退回的商品及执行退款吗?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            inputPattern:
                /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            inputErrorMessage: '请输入退款金额',
            inputPlaceholder: "请输入退款金额",
            center: true,
        }).then((res: any) => {
            arrivalOrderRefund({
                id,
                refund_money: res.value
            })
                .then((res: Result) => {
                    if (typeof _call == "function") {
                        _call()
                    }
                    $utils.successMsg(res);
                })
                .catch((err: ResultError) => {
                    $utils.errorMsg(err);
                });
        });
    }

    return {
        _onAudit,
        _onReceipt,
        _onDelivery,
        _onReceiptAndReFund,
        _onArrival
    }
}